using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Globalization;


/// am eliminat clone pt. ca apareau erori in designer!
/// 
namespace RelationTextBox
{
    public class DGVCalendarCompletCell : DataGridViewTextBoxCell
    {
        private DateTime _MaxValue = DateTime.MaxValue;
        public DateTime MaxValue
        {
            get { return _MaxValue; }
            set { _MaxValue = value;}
        }


        public DGVCalendarCompletCell(): base()
        {
            // Use the short date format.
            // asa a fost pana in data de 30.09.2009
            //this.Style.Format = "d";

            this.Style.Format = "dd.MM.yyyy hh:mm";
            this.MaxValue = _MaxValue;
        }
   
        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            // Set the value of the editing control to the current cell value.
            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
            DGVCalendarCompletEditingControl ctl = DataGridView.EditingControl as DGVCalendarCompletEditingControl;
            CultureInfo cultureRO = new CultureInfo("ro-RO");
            try
            {
                if (ctl.EditingControlValueChanged)
                {
                    Value = ctl.Value;
                }
                else
                {
                    if (initialFormattedValue.ToString() != String.Empty)
                    {
                        ctl.Value = DateTime.Parse(initialFormattedValue.ToString(), cultureRO);
                        //Convert.ToDateTime(initialFormattedValue);
                    }
                    if (ctl.Value > _MaxValue)
                    {
                        ctl.Value = _MaxValue;
                    }
                }

                ctl.MaxDate = MaxValue;
                // nu este cazul sa se modifice valoarea celulei
                /*if (Value != null && ((Value == DBNull.Value) || Value.ToString().Trim() == String.Empty))
                {
                    Value = DateTime.Now;
                }
                if (((DateTime)Value < _MaxValue) && ctl.Value != (DateTime)Value)
                    ctl.Value = (DateTime)Value;
                if (((DateTime)ctl.Value > _MaxValue))
                {
                    ctl.Value = _MaxValue;
                    this.Value = _MaxValue;
                    DataGridView.NotifyCurrentCellDirty(true);
                }*/
            }
            catch
            { 
            
            }
            //ctl.Value = (DateTime)this.DefaultNewRowValue;
            
            
        }

        public override Type EditType
        {
            get
            {
                // Return the type of the editing contol that CalendarCell uses.
                return typeof(DGVCalendarCompletEditingControl);
            }
        }

        public override Type ValueType
        {
            get
            {
                // Return the type of the value that CalendarCell contains.
                return typeof(DateTime);
            }
        }

        public override object DefaultNewRowValue
        {
            get
            {
                // Use the current date and time as the default value.
                return DateTime.Now;
            }
        }

        /// <summary>
        /// Clones a cell, copies all the custom properties.
        /// </summary>
        /*public override object Clone()
        {
            DataGridViewCalendarEditingControl dataGridViewCell = base.Clone() as DataGridViewCalendarEditingControl;
            if (dataGridViewCell != null)
            {
                dataGridViewCell.MaxDate = this.MaxValue;

            }
            return dataGridViewCell;
        }*/
    

    }
}
